<?
class iMVC_Model {



    protected $db;
    protected $properties=array();

    function __construct(){

        $this->db=new iMVC_DB_Mysql('127.0.0.1','root','43253277','amxx');
        


    }

    public function __set($name, $value) {
        $this->properties[$name] = $value;
    }

    public function __get($name) {
        if (array_key_exists($name, $this->properties)) {
            return $this->properties[$name];
        }
    }

    /**  As of PHP 5.1.0  */
    public function __isset($name) {
        return isset($this->properties[$name]);
    }

    /**  As of PHP 5.1.0  */
    public function __unset($name) {
        unset($this->properties[$name]);
    }
    
    public function fetchAll($order='asc'){

        $reader=$this->db->getDbReader();
        $rs=$reader->readAll($this->_table,$this->_pk,$order);
        return $rs;
    }

    public function fetchLimit($start,$count,$orderItem=null,$order='asc'){

        $reader=$this->db->getDbReader();
        $rs=$reader->readByLimit($this->_table,$start,$count,$orderItem,$order);
        return $rs;
    }

    

    //自存
    public function save(){

       $writer=$this->db->getDbWriter(); 
       if($this->properties[$this->_pk]!=null){
            $writer->updateByPk($this->_table,$this->_pk,$this->properties); 
           
       }else{
            $writer->insert($this->_table,$this->properties);
       }

    }
    //自查

    public function findByPk($value){
    
        $reader=$this->db->getDbReader();
        $res=$reader->readByPk($this->_pk,$value,$this->_table);
        if($res!=null){
            $this->properties=array_merge($this->properties,$res);
            return true;
        }
        
        else{
            return false;
        
        }
        
    
    }
    //自删
    public function del(){
    
       $writer=$this->db->getDbWriter(); 
       $writer->deleteByPk($this->_table,$this->_pk,$this->properties[$this->_pk]); 
    
    
    }
    


}


// vim600:ts=4 st=4 foldmethod=marker foldmarker=<<<,>>>
// vim600:syn=php commentstring=//%s
